/**
 * Created by Demon on 2015/4/25.
 */
var IndustryPage = function () {

    var $mar = $("#mar");
    var $mad = $("#mad");
    var $aar = $("#aar");
    var $vdaf = $("#vdaf");
    var $var = $("#var");

    function showBlockUI(message) {
        Metronic.blockUI({
            boxed: true,
            message: message
        });
        window.setTimeout(function () {
            Metronic.unblockUI();
        }, 500);
    }

    var updateMar = function () {
        $.ajax({
            url: basePath + "quality/industry/updateMar",
            type: "post",
            dataType: "json",
            data: "mar=" + $mar.val(),
            success: function () {
                calculateMf();
            }
        })
    };

    var updateMad = function () {
        $.ajax({
            url: basePath + "quality/industry/updateMad",
            type: "post",
            dataType: "json",
            data: "mad=" + $mad.val(),
            success: function () {
                calculateMf();
            }
        });
    };

    var updateArr = function () {
        $.ajax({
            url: basePath + "quality/industry/updateAar",
            type: "post",
            dataType: "json",
            data: "aar=" + $aar.val()
        })
    };

    var updateVdaf = function (isCalculate) {
        $.ajax({
            url: basePath + "quality/industry/updateVdaf",
            type: "post",
            dataType: "json",
            data: "vdaf=" + $vdaf.val(),
            success: function () {
                if(isCalculate)
                calculateVar();
            }
        })
    };

    var updateVar = function (isCalculate) {
        $.ajax({
            url: basePath + "quality/industry/updateVar",
            type: "post",
            dataType: "json",
            data: "var=" + $var.val(),
            success: function () {
                if(isCalculate)
                calculateVdaf();
            }
        });
    };

    var calculateMf = function () {
        if (validate.hasNotNumber([$mar.val(), $mad.val()]))
            return;

        showBlockUI("计算中..");

        $.ajax({
            url: basePath + "quality/industry/calculateMf",
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#mf").val(data.returnMsg);
                $("#resultMf").html(data.returnMsg);
            }
        });
    };

    var calculateVdaf = function () {
        showBlockUI("计算中..");
        $.ajax({
            url: basePath + "quality/industry/calculateVdaf",
            type: "post",
            dataType: "json",
            success: function (data) {
                $vdaf.val(data.returnMsg);
                $("#resultVdaf").html(data.returnMsg);
            }
        });
    };

    var calculateVar = function () {
        showBlockUI("计算中..");
        $.ajax({
            url: basePath + "quality/industry/calculateVar",
            type: "post",
            dataType: "json",
            success: function (data) {
                $var.val(data.returnMsg);
                $("#resultVar").html(data.returnMsg);
            }
        });
    };

    return {
        init: function () {
            $mar.on("blur.save", function () {
                validate.init($mar);
                if (validate.isNotNumber($mar.val())) {
                    validate.invalid();
                    return;
                }

                validate.valid();

                updateMar();

            });

            $mad.on("blur.save", function () {
                validate.init($mad);
                if (validate.isNotNumber($mad.val())) {
                    validate.invalid();
                    return;
                }

                validate.valid();

                updateMad();

            });

            $aar.on("blur.save", function () {
                validate.init($aar);
                if (validate.isNotNumber($aar.val())) {
                    validate.invalid();
                    return;
                }

                validate.valid();

                updateArr();

            });

            $var.on("blur",function(){
               if(!validate.isNotNumber($var.val()))
               updateVar(false);
            });

            $vdaf.on("blur",function(){
               if(!validate.isNotNumber($vdaf.val()))
               updateVdaf(false);
            });

            $("#calculateVdaf").click(function () {
                validate.init($var);
                if (validate.isNotNumber($var.val())) {
                    validate.invalid();
                    return;
                }

                validate.valid();

                updateVar(true);

            });

            $("#calculateVar").click(function () {
                validate.init($vdaf);
                if(validate.isNotNumber($vdaf.val())){
                    validate.invalid();
                    return;
                }

                validate.valid();

                updateVdaf(true);

            });

            $("#save").click(function () {
                window.location.href = basePath + "quality/industry/save";
            });
        },
        recover: function() {
            $.get(basePath + "quality/industry/recover",function(data){
                $mar.val(getValidRecoverVal(data.mar));
                $mad.val(getValidRecoverVal(data.mad));
                $aar.val(getValidRecoverVal(data.aar));
                $vdaf.val(getValidRecoverVal(data.vdaf));
                $var.val(getValidRecoverVal(data.var));
                $("#mf").val(getValidRecoverVal(data.mf));
                $("#resultMf").html(getValidRecoverVal(data.mf));
                $("#resultVdaf").html(getValidRecoverVal(data.vdaf));
                $("#resultVar").html(getValidRecoverVal(data.var));
            });
        }
    }
}();